﻿
<!--#include file="includes/database.asp" -->
<!--#include file="includes/global_varaiable.asp" -->
<!--#include file="theloai_info.asp" -->

<%
Response.expires = 0
Response.expiresabsolute = Now() - 1
Response.addHeader "pragma", "no-cache"
Response.addHeader "cache-control", "private"
Response.addHeader "cache-control", "no-cache"
Response.addHeader "cache-control", "no-store"
Response.CacheControl = "no-cache"

Response.Buffer = True

Const ewSessionMessage = "Message"

' Load key from QueryString
bCopy = True
x_GenreID = Request.QueryString("GenreID")
If x_GenreID = "" Or IsNull(x_GenreID) Then
	bCopy = False
End If

' Get action
sAction = Request.Form("a_add")
If (sAction = "" Or IsNull(sAction)) Then
	If bCopy Then
		sAction = "C" ' Copy record
	Else
		sAction = "I" ' Display blank record
	End If
Else

	' Get fields from form
	x_GenreID = Request.Form("x_GenreID")
	x_Genre = Request.Form("x_Genre")
End If

Select Case sAction
	Case "C": ' Get a record to display
		If Not LoadData() Then ' Load Record based on key
			Session(ewSessionMessage) = "Không có dữ liệu"
			adoCon.Close ' Close adoConection
			Set adoCon = Nothing
			Response.Clear
			Response.Redirect "theloai_list.asp"
		End If
	Case "A": ' Add
		If AddData() Then ' Add New Record
			Session(ewSessionMessage) = "Thêm thành công"
			adoCon.Close ' Close adoConection
			Set adoCon = Nothing
			Response.Clear
			Response.Redirect "theloai_list.asp"
		Else
		End If
End Select


	strSQL = "SELECT * FROM THELOAI"

	' Set up Record Set
	Set rs = Server.CreateObject("ADODB.Recordset")
	rs.CursorLocation = 3
	rs.Open strSql, adoCon, 1, 2
	rs.MoveLast
	
	Dim LastID
	LastID = rs("GenreID")
	LastID = CInt(LastID) + 1
	
	rs.Close
	set rs = Nothing
	
%>

<!--#include file="header.asp"-->

<script type="text/javascript" src="ew.js"></script>

<script type="text/javascript">
<!--
function EW_checkMyForm(EW_this) {
if (EW_this.x_GenreID && !EW_checkinteger(EW_this.x_GenreID.value)) {
	if (!EW_onError(EW_this, EW_this.x_GenreID, "TEXT", "Genre ID phải là số"))
		return false; 
}
if (EW_this.x_Genre && !EW_hasValue(EW_this.x_Genre, "TEXT" )) {
	if (!EW_onError(EW_this, EW_this.x_Genre, "TEXT", "Vui lòng nhập dữ liệu - Genre"))
		return false; 
}
return true;
}
//-->
</script>
<script type="text/javascript">
<!--
	var EW_DHTMLEditors = [];
//-->
</script>
<p><span class="aspmaker">Add to TABLE: THELOAI<br><br><a href="theloai_list.asp">Back to List</a></span></p>
<form name="theloai_add" id="theloai_add" action="theloai_add.asp" method="post" onSubmit="return EW_checkMyForm(this);">
<p>
<input type="hidden" name="a_add" value="A">
<%
If Session(ewSessionMessage) <> "" Then
%>
<p><span class="ewmsg"><%= Session(ewSessionMessage) %></span></p>
<%
	Session(ewSessionMessage) = "" ' Clear message
End If
%>
<table class="ewTable">
	<tr id="r_GenreID">
		<td class="ewTableHeader"><span>Genre ID<span class='ewmsg'>&nbsp;*</span></span></td>
		<td class="ewTableAltRow"><span id="cb_x_GenreID">
<input type="text" name="x_GenreID" id="x_GenreID" size="30" value="<%= Server.HTMLEncode(LastID&"") %>">
</span></td>
	</tr>
	<tr id="r_Genre">
		<td class="ewTableHeader"><span>Genre</span></td>
		<td class="ewTableAltRow"><span id="cb_x_Genre">
<input type="text" name="x_Genre" id="x_Genre" size="30" value="<%= Server.HTMLEncode(x_Genre&"") %>">
</span></td>
	</tr>
</table>
<p>
<input type="submit" name="btnAction" id="btnAction" value="ADD">
</form>
<!--#include file="footer.asp"-->
<%
adoCon.Close ' Close adoConection
Set adoCon = Nothing
%>
<%

'-------------------------------------------------------------------------------
' Function LoadData
' - Load Data based on Key Value
' - Variables setup: field variables

Function LoadData()
	Dim rs, sSql, sFilter
	sFilter = ewSqlKeyWhere
	sFilter = Replace(sFilter, "@GenreID", AdjustSql(x_GenreID)) ' Replace key value
	sSql = ewBuildSql(ewSqlSelect, ewSqlWhere, ewSqlGroupBy, ewSqlHaving, ewSqlOrderBy, sFilter, "")
	Set rs = Server.CreateObject("ADODB.Recordset")
	rs.Open sSql, adoCon
	If rs.Eof Then
		LoadData = False
	Else
		LoadData = True
		rs.MoveFirst

		' Get the field contents
		x_GenreID = rs("GenreID")
		x_Genre = rs("Genre")
	End If
	rs.Close
	Set rs = Nothing
End Function
%>
<%

'-------------------------------------------------------------------------------
' Function AddData
' - Add Data
' - Variables used: field variables

Function AddData()
	Dim rs, sSql, sFilter
	Dim bCheckKey, sSqlChk, sWhereChk
	sFilter = ewSqlKeyWhere

	' Check for duplicate key
	bCheckKey = True
	If x_GenreID = "" Or IsNull(x_GenreID) Then
		bCheckKey = False
	Else
		sFilter = Replace(sFilter, "@GenreID", AdjustSql(x_GenreID)) ' Replace key value
	End If
	If bCheckKey Then
		sSqlChk = ewBuildSql(ewSqlSelect, ewSqlWhere, ewSqlGroupBy, ewSqlHaving, ewSqlOrderBy, sFilter, "")
		Set rsChk = adoCon.Execute(sSqlChk)
		If Not rsChk.Eof Then
			Session(ewSessionMessage) = "Genre ID đã tồn tại, vui lòng kiểm tra lại"
			rsChk.Close
			Set rsChk = Nothing
			AddData = False
			Exit Function
		End If
		rsChk.Close
		Set rsChk = Nothing
	End If
	If x_GenreID = "" Or IsNull(x_GenreID) Then ' Check field with unique index

		' Ignore
	Else
		sFilter = "([GenreID] = " & AdjustSql(x_GenreID) & ")"
		sSqlChk = ewBuildSql(ewSqlSelect, ewSqlWhere, ewSqlGroupBy, ewSqlHaving, ewSqlOrderBy, sFilter, "")
		Set rsChk = adoCon.Execute(sSqlChk)
		If Not rsChk.Eof Then
			Session(ewSessionMessage) = "Trùng dữ liệu -- [GenreID], value = " & x_GenreID
			rsChk.Close
			Set rsChk = Nothing
			AddData = False
			Exit Function
		End If
		rsChk.Close
		Set rsChk = Nothing
	End If

	' Add New Record
	sFilter = "(0 = 1)"
	sSql = ewBuildSql(ewSqlSelect, ewSqlWhere, ewSqlGroupBy, ewSqlHaving, ewSqlOrderBy, sFilter, "")
	Set rs = Server.CreateObject("ADODB.Recordset")
	rs.CursorLocation = 3
	'rs.CursorType = 3
	'rs.LockType = 3	
	rs.Open sSql, adoCon, 1, 2
	
	' Clone new rs object
	Dim rsnew
	Set rsnew = rs.Clone(1)
	rs.AddNew
		
	'response.write(x_GenreID & " " & x_Genre)
	
	'response.write(sSql)

	' Field GenreID
	sTmp = CInt(x_GenreID)
	If Not IsNumeric(sTmp) Then
		sTmp = 0
	Else
		sTmp = CLng(sTmp)
	End If
	rs("GenreID") = sTmp

	' Field Genre
	sTmp = Trim(x_Genre)
	If Trim(sTmp) = "" Then sTmp = Null
	rs("Genre") = sTmp

	' Call recordset inserting event
	If Recordset_Inserting(rs) Then
		rs.Update
		AddData = True
	Else
		rs.CancelUpdate
		AddData = False
	End If
	rs.Close
	Set rs = Nothing

	' Call recordset inserted event
	If AddData Then
		Call Recordset_Inserted(rsnew)
	End If
	rsnew.Close
	Set rsnew = Nothing
End Function

'-------------------------------------------------------------------------------
' Recordset inserting event

Function Recordset_Inserting(rsnew)
	On Error Resume Next

	' Please enter your customized codes here
	Recordset_Inserting = True
End Function

'-------------------------------------------------------------------------------
' Recordset inserted event

Sub Recordset_Inserted(rsnew)
	On Error Resume Next

	' Get key value
	Dim sKey
	sKey = ""
	If sKey <> "" Then sKey = sKey & ","
	sKey = sKey & rsnew.Fields("GenreID")
End Sub
%>
